package com.second.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.second.mapper.FShopMapper;
import com.second.mapper.FShopDao;
import com.second.model.pojo.FGoods;
import com.second.model.pojo.FShop;
import com.second.service.IFShopService;
import com.second.util.AmapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
@Service
@Transactional
public class FShopServiceImpl implements IFShopService {

    @Autowired
    private FShopDao shopDao;

    @Autowired
    private FShopMapper shopMapper;

    @Override
    public ArrayList<FShop> findAll() {
        return shopMapper.findAll();
    }

    @Override
    public FShop getById(Integer id) {
        return shopMapper.getById(id);
    }

    @Override
    public List<FShop> orderByScoreDesc() {
        List<FShop> list = shopMapper.findAll();
        Collections.sort(list, new Comparator<FShop>() {
            @Override
            public int compare(FShop o1, FShop o2) {
                if (o1.getShopScore()>o2.getShopScore()){
                    return -1;
                }else return 1;
            }
        });
        return list;
    }

    //    模糊查询
    @Override
    public PageInfo<FShop> search(String shopName,Integer pageNum,Integer pageSize) {
        PageInfo<FShop> pageInfo = null;
        PageHelper.startPage(pageNum,pageSize);
        List<FShop>  shop=shopMapper.selectByShopName(shopName);
        pageInfo=new PageInfo<>(shop);

        return  pageInfo;
    }
    //根据价格查询
    @Override
    public PageInfo<FShop> price(Double start, Double end,Integer pageNum,Integer pageSize) {
        PageInfo<FShop> pageInfo = null;
        PageHelper.startPage(pageNum,pageSize);
        List<FShop> shops=shopMapper.selectByPrice(start,end);
        pageInfo=new PageInfo<>(shops);
        return pageInfo;
    }




    @Override
    public PageInfo<FShop> findByType(Integer type,Integer pageNum,Integer pageSize) {
        PageInfo<FShop> pageInfo = null;
        PageHelper.startPage(pageNum,pageSize);
        List<FShop> list=shopMapper.findByType(type);
        pageInfo = new PageInfo<>(list);
        return pageInfo;
    }


    @Override
    public PageInfo<FShop> findAllByArea(String area,Integer pageNum,Integer pageSize) {
        PageInfo<FShop> pageInfo = null;
        PageHelper.startPage(pageNum,pageSize);
        List<FShop> list=shopMapper.findAllByArea(area);
        pageInfo=new PageInfo<>(list);
        return pageInfo;
    }

    //查看所有并按评分排序
    @Override
    public PageInfo<FShop> findAllByShopScore(Integer pageNum,Integer pageSize) {
        PageInfo<FShop> pageInfo = null;
        PageHelper.startPage(pageNum,pageSize);
        List<FShop> list=shopMapper.findAllByScoreDesc();
        pageInfo=new PageInfo<>(list);

        return pageInfo;
    }

    //保存店铺评分
    @Override
    public void saveScore(FShop shop,Double score) {
        shop.setShopScore(score);
        shopDao.save(shop);
    }

    /**
     * 根据ShopId查询店铺的具体位置
     * @param id
     * @return
     */
    @Override
    public String coordinate(Integer id) {
        FShop fShop = shopMapper.getById(id);
        String str = "";
        str += fShop.getProvince();
        str += fShop.getCity();
        str += fShop.getArea();
        str += fShop.getShopName();
        return AmapUtil.coordinate(str);
    }

    @Override
    public List<FShop> findShopsByTypes(Integer type,Integer shopId) {
        List<FShop> shopList = shopMapper.findByType(type);
        shopList.removeIf(fs -> fs.getId() == shopId);
        return shopList;
    }

    @Override
    public List<FShop> findByName(String shopName) {
        List<FShop> shopList=shopMapper.selectByShopName(shopName);
        return shopList;
    }
}
